package com.bana.fvs.protocol;

import com.bana.fvs.code.RelayControlMode;

import java.io.*;

/**
 * 查询继电器状态
 */
public class QueryRelayStatus {

    public int getId()
    {
        return 0x10;
    }

    public byte[] toBuffer()
    {
        byte[] buffer=new byte[8];
        buffer[0]=0x55;
        buffer[1]=0x01;
        buffer[2]=0x10;
        buffer[3]=0x00;
        buffer[4]=0x00;
        buffer[5]=0x00;
        buffer[6]=0x00;
        buffer[7]=0x66;
        return buffer;
    }


    public int toModel(byte[] buffer) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
        DataInputStream dis = new DataInputStream(bais);
        dis.skipBytes(6);
        byte b=dis.readByte();
        int result;
        if((b&0x02)==0)
        {
           if((b&0x01)==0)
           {
               result= RelayControlMode.Main;
           }
           else
           {
               result= RelayControlMode.Bak;
           }
        }
        else
        {
            result= RelayControlMode.Off;
        }

        dis.close();
        bais.close();
        return result;
    }
}
